//
//  LPActivitySearchViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/10.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivitySearchViewController.h"
#import "ActivityJoinTableViewCell.h"
#import "ActivityJoinDetailModel.h"
#import "LPActivityDetailViewController.h"
#import "LPCostomHeadLineAndTextViewAndTiShiView.h"
@interface LPActivitySearchViewController ()<UITableViewDelegate,UITableViewDataSource,ActivityJoinCellDelegate,LPCostomHeadLineAndTextViewAndTiShiDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**  搜索的关键字  */
@property (strong, nonatomic) NSString  *keyWord;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  jc  */
@property (strong, nonatomic) JCAlertView  *jcAlertView;
/**  tishiRow  */
@property (assign, nonatomic) NSInteger  tishiRow;
/**    */
@property (assign, nonatomic) NSInteger  tishiSection;
/**  model  */
@property (strong, nonatomic) ActivityJoinDetailModel  *chooseModel;

@end

@implementation LPActivitySearchViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyWord = @"";
    /**  添加通知  */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchText:) name:@"post" object:nil];
       __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self layoutUI];
    
    self.keyWord = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchContent"];
    if (self.keyWord.length != 0) {
        self.tableView.mj_header.hidden = NO;
        [self.tableView.mj_header beginRefreshing];
        
    }else{
        self.tableView.mj_header.hidden  = YES;
        self.tableView.mj_footer.hidden = YES;
        
        
    }

}
#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindActivityForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ACTS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array] mutableCopy];
            self.tableView.mj_footer.hidden = NO;
            [weakSelf.tableView reloadData];
            
        }else{
            [YJProgressHUD showSuccess:object[@"ERROR_MSG"] inview:self.view];
            [self.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
            [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else{
            [YJProgressHUD showSuccess:@"加载数据失败" inview:weakSelf.view];
        }
    }];
    
}

- (void)getData{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindActivityForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ACTS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array] mutableCopy];
            self.tableView.mj_footer.hidden = NO;
            [weakSelf.tableView reloadData];
        }else{
            [YJProgressHUD showSuccess:object[@"ERROR_MSG"] inview:self.view];
            [self.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
        }
        
    } Fail:^(id object) {
        
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindActivityForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ACTS"];
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
                
            }
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 数据

- (void)searchText:(NSNotification *)nioti{
    self.keyWord = nioti.userInfo[@"text"];
    if (self.keyWord.length == 0) {
        self.tableView.mj_header.hidden = YES;
        self.tableView.mj_footer.hidden = YES;
        [self.dataSouce removeAllObjects];
        [self.tableView reloadData];
        
    }else{
        self.tableView.mj_header.hidden  = NO;
        [self.tableView.mj_header beginRefreshing];
        
        
    }
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
    header.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = header;
}



#pragma mark - ActivityJoinCellDelegate
- (void)ActivityJoinClick_index:(NSInteger)index section:(NSInteger)section statusText:(NSString *)statusText{
    __weak typeof(self) weakSelf = self;
    ActivityJoinDetailModel *model = self.dataSouce[index];
    if ([statusText isEqualToString:@"加入"]) {
        [YJProgressHUD showMessage:@"报名中..." duration:10 view:self.view];
        /**  这是点击加入  */
        NSDictionary *paramer = @{@"AI_ID": model.ID,
                                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                  @"USER_NAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME};
        [NetWorkingManager postWithUrlString:[NetWorkingURL getactactionMobileEnroll] Parameter:paramer Success:^(id object) {
            if ([object[@"CODE"] isEqualToString:@"20000"]) {
                [MobClick event:[UMMStatisticsString registerAct]];
                [YJProgressHUD showSuccess:@"报名" inview:weakSelf.view];
                /**  报名成功修改数据源刷新界面  */
                [weakSelf.tableView.mj_header beginRefreshing];
                
            }else if ([object[@"CODE"] isEqualToString:@"80001"]) {
                [YJProgressHUD showSuccess:@"重复报名" inview:weakSelf.view];
            }else if ([object[@"CODE"] isEqualToString:@"80002"]) {
                [YJProgressHUD showSuccess:@"活动结束" inview:weakSelf.view];
            }else if ([object[@"CODE"] isEqualToString:@"80003"]) {
                [YJProgressHUD showSuccess:@"人数达上限" inview:weakSelf.view];
            }else if ([object[@"CODE"] isEqualToString:@"80004"]) {
                [YJProgressHUD showSuccess:@"无此活动或已被删除" inview:weakSelf.view];
            }else{
                [YJProgressHUD showSuccess:@"报名失败" inview:weakSelf.view];
            }
            
        } Fail:^(id object) {
           [YJProgressHUD showSuccess:@"报名失败" inview:weakSelf.view];
        }];
        
        
    }else if([statusText isEqualToString:@"请假"]){
        LPCostomHeadLineAndTextViewAndTiShiView *tishi = [[LPCostomHeadLineAndTextViewAndTiShiView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, [UIScreen zj_screenHeight]) headLine:@"请输入原因"];
        tishi.delegate = self;
        self.jcAlertView = [[JCAlertView alloc] initWithCustomView:tishi dismissWhenTouchedBackground:YES];
        [self.jcAlertView show];
        self.chooseModel = model;
        self.tishiSection = section;
        self.tishiRow = index;
        
    }
}

#pragma mark - LPCostomHeadLineAndTextViewAndTiShiDelegate
- (void)LPCostomHeadLineAndTextViewAndTiShiWithCancle:(BOOL)cancle{
    [self.jcAlertView dismissWithCompletion:nil];
}

- (void)LPCostomHeadLineAndTextViewAndTiShiWithString:(NSString *)string{
    __weak typeof(self) weakSelf = self;
    /**   这里是请假  */
    [YJProgressHUD showMessage:@"请假中" duration:10 view:self.view];
    NSDictionary *paramer = @{@"AI_ID": self.chooseModel.ID,
                              @"FROM_USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"FROM_USERNAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,
                              @"FROM_UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"PMID": ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID,
                              @"CONTENT": string,
                              @"TO_UNITID": self.chooseModel.UNITID,
                              @"FROM_UNITNAME": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.DEPT_NAME,
                              @"ACT_NAME": self.chooseModel.NAME};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActLeave] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
           [YJProgressHUD showSuccess:@"请假成功" inview:weakSelf.view];
            /**  请假成功修改数据源刷新界面  */
            self.chooseModel.IS_LEAVE = @"1";
            self.chooseModel.LEAVE_STATE = @"0";
            weakSelf.dataSouce[self.tishiRow] = self.chooseModel;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tishiRow inSection:self.tishiSection];
            [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
           [YJProgressHUD showSuccess:@"请假失败" inview:weakSelf.view];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showSuccess:@"请假失败" inview:weakSelf.view];
    }];
    
    [self.jcAlertView dismissWithCompletion:nil];
    
}


/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ActivityJoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ActivityJoinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.isNoShow = YES;
    cell.model = self.dataSouce[indexPath.row];
    cell.delegate = self;
    cell.index = indexPath.row;
    cell.section = indexPath.section;
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 50 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}


@end
